package com.hj.system.system.config;

import com.hj.system.system.filter.RemoteIpFilter;
import com.hj.system.system.filter.XssFilter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import java.util.HashMap;
import java.util.Map;

/**
 * 过滤器配置
 */
@Configuration
@EnableConfigurationProperties(SystemProperties.class)
public class SystemConfig {
    /**
     * websocket
     */
   // @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
    /**
     * 过滤器放行路径
     * */
    private Map<String, String> initParameters = new HashMap<>();

    {
        initParameters.put("excludes", "/favicon.ico,/img/*,/js/*,/css/*,/static/*");
        initParameters.put("isIncludeRichText", "true");
    }

    /**
     * xss过滤拦截器
     */
    @Bean
    public FilterRegistrationBean xssFilterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new XssFilter());
        filterRegistrationBean.setOrder(2);
        filterRegistrationBean.setEnabled(false);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setInitParameters(initParameters);
        return filterRegistrationBean;
    }

    /**
     * 远程ip过滤拦截器
     */
    @Bean
    public FilterRegistrationBean remoteIpFilterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new RemoteIpFilter());
        filterRegistrationBean.setOrder(1);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setInitParameters(initParameters);
        return filterRegistrationBean;
    }
}
